<?php
/**
 * Created by PhpStorm.
 * User: chenxinghua
 * Date: 2018/4/13
 * Time: 16:33
 */

namespace app\common;
class ResponseFormat {

    // 当前的contentType
    //protected static $content_type = 'text/html';
    // 字符集
    //protected static  $charset = 'utf-8';


    const SUCCESS = 10000; //请求成功
    const FAILED = 20000;  //请求失败或服务不可用，如发生异常等情况（前面的业务逻辑都是正确的，但是最终还是失败的情况）
    const PERMISSION_DENIED = 30000; //权限不足
    const APP_TOKEN_EXPIRED = 30001; //app_token过期（app的token）
    const MEMBER_TOKEN_EXPIRED = 30002; //member_token过期（用户权限的token）
    const MISSING_PARAMETER = 40000; //缺少参数
    const ILLEGAL_PARAMETER = 40001; //参数不合法
    const BUSINESS_LOGIC_RESTRICT = 50000; //业务逻辑限制
    const DATA_NOT_FOUND = 50001; //数据不存在 仅用于查询的接口


    protected static $codes = [
        self::SUCCESS                 => 'SUCCESS',
        self::FAILED                  => 'FAILED',
        self::PERMISSION_DENIED       => 'PERMISSION_DENIED',
        self::APP_TOKEN_EXPIRED       => 'APP_TOKEN_EXPIRED',
        self::MEMBER_TOKEN_EXPIRED    => 'MEMBER_TOKEN_EXPIRED',
        self::MISSING_PARAMETER       => 'MISSING_PARAMETER',
        self::ILLEGAL_PARAMETER       => 'ILLEGAL_PARAMETER',
        self::BUSINESS_LOGIC_RESTRICT => 'BUSINESS_LOGIC_RESTRICT',
        self::DATA_NOT_FOUND          => 'DATA_NOT_FOUND',
    ];

    protected static $messages = [
        self::SUCCESS                 => '请求成功',
        self::FAILED                  => '请求失败',
        self::PERMISSION_DENIED       => '权限不足',
        self::APP_TOKEN_EXPIRED       => 'app_token过期',
        self::MEMBER_TOKEN_EXPIRED    => 'member_token过期',
        self::MISSING_PARAMETER       => '缺少参数',
        self::ILLEGAL_PARAMETER       => '参数不合法',
        self::BUSINESS_LOGIC_RESTRICT => '业务逻辑限制',
        self::DATA_NOT_FOUND          => '数据不存在',
    ];


    private static $response = [
        'code' => '',
        'msg'  => '',
        'data' => []
    ];


    public static function getCodeName($code) {
        if (!isset(static::$codes[$code])) {
            throw new \InvalidArgumentException('Code "' . $code . '" is not defined, use one of: ' . implode(', ', array_keys(static::$codes)));
        }

        return static::$codes[$code];
    }

    public static function getMessages($code) {
        if (!isset(static::$messages[$code])) {
            throw new \InvalidArgumentException('Code "' . $code . '" is not defined, use one of: ' . implode(', ', array_keys(static::$codes)));
        }

        return static::$messages[$code];
    }


    /**
     * @date 2019/6/26 11:47
     * @description 设置当前成功状态数组，并返回
     * @author cxh
     * @param array $data
     * @param int $code
     * @param string $msg
     * @return array
     */
    public static function success_array(array $data = [], $code = self::SUCCESS, $msg = null) {
        $response = [
            'code' => $code,
            'msg'  => $msg ?: self::getMessages($code),
            'data' => $data
        ];
        self::$response = $response;
        return $response;
    }

    /**
     * @date
     * @description 设置当前成功状态数组，并返回
     * @author cxh
     * @param array $data
     * @param int $code
     * @param string $msg
     * @return string
     */
    /*public static function success_json(array $data = [], $code = self::SUCCESS, $msg = null) {
        $response = [
            'code' => $code,
            'msg'  => $msg ?: self::getMessages($code),
            'data' => $data
        ];
        self::$response = $response;
        return json_encode($response);
    }*/

    /**
     * @date
     * @description 设置当前失败状态数组，并返回
     * @author cxh
     * @param array $data
     * @param int $code
     * @param string $msg
     * @return array
     */
    public static function failed_array($code = self::FAILED, $msg = null, array $data = []) {
        $response = [
            'code' => $code,
            'msg'  => $msg ?: self::getMessages($code),
            'data' => $data
        ];
        self::$response = $response;
        return $response;
    }

    /**
     * @date
     * @description 设置当前失败状态数组，并返回json
     * @author cxh
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return string
     */
    public static function failed_json($code = self::FAILED, $msg = null, array $data = []) {
        $response = [
            'code' => $code,
            'msg'  => $msg ?: self::getMessages($code),
            'data' => $data
        ];
        self::$response = $response;
        return json_encode($response);
    }

    /**
     * @date
     * @description 获取当前数组
     * @author cxh
     * @return array
     */
    public static function get_current_format_data() {
        return self::$response;
    }


    /*public static function set_header(){
        $content_type = self::$content_type;
        $charset = self::$charset;
        header( "Content-Type:{$content_type};charset={$charset}");
    }*/
}